package com.songwp.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.songwp.common.CommonResult;
import com.songwp.entity.User;
import com.songwp.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @ClassName: UserController
 * @Description:
 * @Author: songwp
 * @Date: 16:12 2022/4/18
 **/
@RestController
@RequestMapping("/user")
public class UserController{

    @Resource
    UserMapper userMapper;

    /**
     * 新增用户信息
     * @param user
     * @return
     */
    @PostMapping("/addUser")
    public CommonResult addUser(@RequestBody User user){
        userMapper.insert(user);
        return CommonResult.success();
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @PutMapping("/editUser")
    public CommonResult editUser(@RequestBody User user){
        userMapper.updateById(user);
        return CommonResult.success();
    }

    /**
     * 删除用户信息
     * @param id
     * @return
     */
    @DeleteMapping("/deleteUser/{id}")
    public CommonResult deleteUser(@PathVariable Integer id){
        userMapper.deleteById(id);
        return CommonResult.success();
    }

    /**
     * 获取用户列表信息
     * @param pageNum
     * @param pageSize
     * @param search
     * @return
     */
    @GetMapping("/getUserList")
    public CommonResult getUserList(@RequestParam(defaultValue = "1") Integer pageNum,
                                    @RequestParam(defaultValue = "10") Integer pageSize,
                                    @RequestParam(defaultValue = "") String search){
        LambdaQueryWrapper<User> wrapper = Wrappers.<User>lambdaQuery();
        if (StrUtil.isNotBlank(search)){
            wrapper.like(User::getNickname,search);
        }
        Page<User> userPage = userMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return CommonResult.success(userPage);
    }
}
